<template>
  <el-container class="layout">
    <el-header class="header">
      <div class="left">
        <i class="el-icon-s-fold" @click="isCollapse = !isCollapse"></i>
        <img src="@/assets/img/login-title.png" class="logo" alt />
        <span class="title">黑马面面</span>
      </div>

      <div class="right">
        <img :src="userInfo.avatar" class="avatar" alt />
        <span class="username">{{$store.state.userInfo.username}}，您好</span>
        <el-button type="primary" size="mini" @click="logoutClick">退出</el-button>
      </div>
    </el-header>

    <el-container>
      <el-aside width="auto">
        <el-menu
          :collapse="isCollapse"
          :router="true"
          :default-active="$route.path"
          class="el-menu-vertical-demo"
        >

          <el-menu-item :index="'/home/'+item.path" v-for="(item, index) in $router.options.routes[2].children" :key="index" v-show="item.meta.rules.includes($store.state.role)">
            <i :class="item.meta.icon"></i>
            <span slot="title">{{item.meta.title}}</span>
          </el-menu-item>

          <!-- <el-menu-item index="/home/chart">
            <i class="el-icon-time"></i>
            <span slot="title">数据概览</span>
          </el-menu-item>
          <el-menu-item index="/home/userList">
            <i class="el-icon-user"></i>
            <span slot="title">用户列表</span>
          </el-menu-item>
          <el-menu-item index="/home/question">
            <i class="el-icon-edit-outline"></i>
            <span slot="title">题库列表</span>
          </el-menu-item>
          <el-menu-item index="/home/business">
            <i class="el-icon-office-building"></i>
            <span slot="title">企业列表</span>
          </el-menu-item>
          <el-menu-item index="/home/subject">
            <i class="el-icon-notebook-2"></i>
            <span slot="title">学科列表</span>
          </el-menu-item> -->
        </el-menu>
      </el-aside>

      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { getUserInfo, logout } from "@/api/home.js";
import { removeToken } from "@/utils/token.js";
export default {
  data() {
    return {
      // 用户信息
      userInfo: {
        avatar: ""
      },
      // 是否折叠菜单
      isCollapse: false
    };
  },

  created() {
    console.log('路由管理器', this.$router);
    getUserInfo().then(res => {
      console.log('用户信息', res); 
      this.userInfo = res.data;
      this.userInfo.avatar = process.env.VUE_APP_URL + "/" + res.data.avatar;
      this.$store.state.userInfo = this.userInfo;
      this.$store.state.role = res.data.role
      // 判断：如果用户 为禁用状态，提示下用户，
      if (this.$store.state.userInfo.status == 0){
        this.$message.warning('您的账号已被禁用，请联系管理员')
        // 清除token
        removeToken()
        // 跳转至登录页
        this.$router.push('/')
      }else{
        // 判断用户有无权限访问此页面
        if(!this.$route.meta.rules.includes(res.data.role)){
          this.$message.warning('您没有权限访问此页面')
          // 删除token
          removeToken()
          // 跳转至登录页
          this.$router.push('/')
        }
      }
    });
  },

  methods: {
    logoutClick() {
      // 弹出MessageBox弹框
      this.$confirm("确定要退出登录吗？？？集美", "提示", {
        confirmButtonText: "退出",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          logout().then(res => {
            console.log(res);
            this.$message.success("成功退出登录");
            this.$router.push("/login");
            // 删除token
            removeToken();
          });
        })
        .catch(() => {});
    }
  }
};
</script>

<style lang="less">
.layout {
  background-color: #e8e9ec;
  height: 100%;

  .header {
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .left {
    display: flex;
    align-items: center;
    .el-icon-s-fold {
      font-size: 24px;
    }

    .logo {
      width: 35px;
      height: 30px;
      margin: 0 15px;
    }

    .title {
      font-size: 24px;
      color: #0f9dfa;
    }
  }

  .right {
    .avatar {
      width: 45px;
      height: 45px;
      vertical-align: middle;
    }

    .username {
      margin: 0 60px 0 15px;
    }
  }

  ul.el-menu-vertical-demo.el-menu {
    height: 100%;
  }
  .el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 200px;
  }
}
</style>